package com.yangtao.prototype.master.constant.service.impl;

import com.yangtao.framework.common.Option;
import com.yangtao.framework.service.BaseService;
import com.yangtao.framework.util.StringHelper;
import com.yangtao.prototype.master.constant.service.ConstantService;
import com.yangtao.prototype.master.constant.web.ConstantModel;
import com.yangtao.prototype.master.dao.ConstantDao;
import com.yangtao.prototype.master.domain.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ConstantServiceImpl extends BaseService implements ConstantService {
	@Autowired
	protected ConstantDao constantDao;

	@Override
	public ConstantModel getAllList(ConstantModel model) {
		model.setConstantList(constantDao.getAllList(model.getUser().getCompanyId()));
		return model;
	}

	@Override
	public ConstantModel getList(ConstantModel model) {
		model.getPage().setItems(constantDao.findAll());
		return model;
	}

	@Override
	public ConstantModel saveConstant(ConstantModel model) {
		// 增加新增的列表
		for (Constant c : model.getNewItems()) {
			constantDao.save(c);
		}
		// 修改修改列表
		constantDao.updateAll(model.getModifiedItems());
		// 删除已经删除的列表
		for (Constant c : model.getDeletedItems()) {
			constantDao.delete(c.getId());
		}
		model.addInfo("保存成功");
		return model;
	}

	@Override
	public ConstantModel loadConstant(ConstantModel model) {
		if (StringHelper.isNotEmpty(model.getConstant().getId())) {
			model.setConstant(constantDao.loadForReview(model.getConstant().getId()));
		}
		return model;
	}

	@Override
	public ConstantModel findConst(ConstantModel model) {
		String code = model.getCode();
		model.getPage().setItems(constantDao.findByCode(code));
		return model;
	}

	@Override
	public List<Option> findByType(String type) {
		return constantDao.findByType(type);
	}

	@Override
	public String findValue(String type, String code) {
		return constantDao.findValue(type, code);
	}

}
